package lab6;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Flow {
	

	public static void main(String[] args) {
		int[][] graph = new int[1][1];
		int nbrOfNodes = 0;
		int source = 0;
		int sink = 0;
		
		try {
			FileInputStream is = new FileInputStream(args[0]);
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			
			nbrOfNodes = Integer.parseInt(br.readLine());
			System.out.println("Nbr of nodes " + nbrOfNodes);
			graph = new int[nbrOfNodes + 1][nbrOfNodes + 1];
			source = 0;
			sink = nbrOfNodes - 1;
			
			int u, v, capacity;
			String[] str;
			
			for(int i = 0;  i < nbrOfNodes; i++) {
				br.readLine();
			}
			
			int nbrOfArcs = Integer.parseInt(br.readLine());
			System.out.println("Nbr of arcs " + nbrOfArcs);
			for(int i = 0; i < nbrOfArcs; i++) {
				str = br.readLine().split(" ");
				u = Integer.parseInt(str[0]);
				v = Integer.parseInt(str[1]);
				capacity = Integer.parseInt(str[2]);
				if(capacity == -1)
					capacity = Integer.MAX_VALUE;
				
				graph[u][v] = capacity;
				graph[v][u] = capacity;
			}
			
			System.out.println();
			for(int i = 0; i <= nbrOfNodes; i++) {
				for(int j = 0; j < nbrOfNodes; j++) {
					System.out.print(graph[i][j] + " ");
				}
				System.out.println();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		FordFulkerson ff = new FordFulkerson(nbrOfNodes);
		System.out.println("\nMax flow: " + ff.maxFlow(graph, source, sink) + "\n");

	}

}
